package ynu.edu.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.domain.R;
import ynu.edu.common.utils.UserContext;
import ynu.edu.user.domain.vo.UserVO;
import ynu.edu.user.service.IUserService;

@RestController
@RequestMapping("/profile")
@RequiredArgsConstructor
@Api(tags = "用户信息接口")
@Slf4j
public class ProfileController {

    private final IUserService userService;

    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public R<UserVO> getProfileInfo() {
        Long id = UserContext.getUser();
        log.info("获取用户信息，用户ID：{}", id);
        return userService.queryUserById(id);
    }

    @PutMapping("/info")
    @ApiOperation("更新用户信息")
    public R<Void> updateUser(@RequestBody UserVO userVO) {
        Long id = UserContext.getUser();
        log.info("更新用户信息，用户ID：{}", id);
        userService.updateUser(id, userVO);
        return R.ok();
    }

    @PutMapping("/password")
    @ApiOperation("更新用户密码")
    public R<Void> updatePassword(@RequestParam("oldPw") String oldPw, @RequestParam("newPw") String newPw) {
        userService.updatePassword(oldPw, newPw);
        return R.ok();
    }
}
